
export default {

    /**
     * 用于浏览器滚动刷新获取的相关容器高度数据
     */

    //获取当前可视范围的高度
    getClientHeight() {
        var clientHeight = 0;
        if (document.body.clientHeight && document.documentElement.clientHeight) {
            clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight)
        } else {
            clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight)
        }
        return clientHeight
    },

    //获取滚动条顶部到滚动条容器顶部的的高度
    getScrollHeight() {
        return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)
    },

    //获取当前滚动条的位置
    getScrollTop() {
        var scrollTop = 0;
        //window.pageYOffset = document.documentElement.scrollTop
        if (document.documentElement && document.documentElement.scrollTop) {
            scrollTop = document.documentElement.scrollTop
        } else if (document.body) {
            scrollTop = document.body.scrollTop
        }
        return scrollTop
    },

    /**
     * 将时间转为xx分钟、小时……前
     * 
     *  "time": {
        "month": "MARCH",
        "year": 2023,
        "dayOfMonth": 21,
        "hour": 17,
        "minute": 57,
        "monthValue": 3,
        "nano": 0,
        "second": 55,
        "dayOfWeek": "TUESDAY",
        "dayOfYear": 80,
        "chronology": {
            "id": "ISO",
            "calendarType": "iso8601"
        }
    }
     */
    transTime(time) {
        let value = time.year + "-" + time.monthValue + "-" + time.dayOfMonth + "-" + " " + time.hour + ":" + time.minute + ":" + time.second;

        // 拿到当前时间戳和发布时的时间戳
        var curTime = new Date()
        var postTime = new Date(value)
        //计算差值
        var timeDiff = curTime.getTime() - postTime.getTime()
        // 单位换算
        var min = 60 * 1000
        var hour = min * 60
        var day = hour * 24
        // 计算发布时间距离当前时间的 天、时、分
        var exceedDay = Math.floor(timeDiff / day)
        var exceedHour = Math.floor(timeDiff / hour)
        var exceedMin = Math.floor(timeDiff / min)
        // 最后判断时间差
        if (exceedDay > 0 && exceedDay <= 364) {
            return time.monthValue + "-" + time.dayOfMonth
        } else {
            if (exceedDay > 364) {
                return time.year + "-" + time.monthValue + "-" + time.dayOfMonth
            }
            if (exceedHour < 24 && exceedHour > 0) {
                return exceedHour + '小时前'
            } else if (exceedMin < 60 && exceedMin > 0) {
                return exceedMin + '分钟前'
            } else {
                return '刚刚'
            }
        }
    },


    //模拟sleep 放在异步函数里await同步执行
    setSleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

}
